package cn.edu.xmu.oomall.comment.controller;

import cn.edu.xmu.javaee.core.aop.Audit;
import cn.edu.xmu.javaee.core.aop.LoginUser;
import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.core.model.Constants;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.javaee.core.model.ReturnObject;
import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.javaee.core.model.vo.PageVo;
import cn.edu.xmu.oomall.comment.controller.dto.CommentDto;
import cn.edu.xmu.oomall.comment.controller.vo.CommentVo;
import cn.edu.xmu.oomall.comment.dao.bo.Comment;
import cn.edu.xmu.oomall.comment.service.CommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import cn.edu.xmu.javaee.core.util.CloneFactory;

import java.util.List;
import java.util.Objects;

@RestController /*Restful的Controller对象*/
@RequestMapping(value = "/shops/{id}", produces = "application/json;charset=UTF-8")
@Transactional
@RequiredArgsConstructor
public class ShopCommentController {
    private final CommentService commentService;

    /**
     * 管理员审核评论
     * @param id 店铺id
     * @param cid 评论id
     * @param dto 评论信息
     * @param user 用户
     */
    @Audit(departName = "shops")
    @PutMapping("/comments/{cid}")
    public ReturnObject reviewComment(@PathVariable Long id,
                                      @PathVariable Long cid,
                                      @RequestBody CommentDto dto,
                                      @LoginUser UserDto user){
        if(!Objects.equals(id, Constants.PLATFORM)){
            throw new BusinessException(ReturnNo.AUTH_NO_RIGHT, String.format(ReturnNo.AUTH_NO_RIGHT.getMessage(), "审核评论"));
        }
        Comment comment = CloneFactory.copy((new Comment()), dto);
        comment.setId(cid);
        commentService.reviewComment(comment, user);
        return new ReturnObject(ReturnNo.OK, String.format(ReturnNo.OK.getMessage(), "审核评论"));
    }

    /**
     * 商户回复评论
     * @param id 店铺id
     * @param cid 评论id
     * @param dto 评论信息
     * @param user 用户
     */
    @Audit(departName = "shops")
    @PostMapping("/comments/{cid}/reply")
    public ReturnObject replyComment(@PathVariable Long id,
                                     @PathVariable Long cid,
                                     @RequestBody CommentDto dto,
                                     @LoginUser UserDto user){
        Comment replyComment = CloneFactory.copy(new Comment(), dto);
        replyComment.setPid(cid);
        Comment comment = commentService.replyComment(replyComment, user);
        return new ReturnObject(ReturnNo.CREATED, CloneFactory.copy(new CommentVo(comment), comment));
    }

    /**
     * 管理员查询评论
     * @param id 店铺id
     * @param status 评论状态
     */
    @Audit(departName = "shops")
    @GetMapping("/comments/states/{status}")
    public ReturnObject findCommentsByStatus(@PathVariable Long id,
                                     @PathVariable Byte status,
                                     @RequestParam(required = false, defaultValue = "1") Integer page,
                                     @RequestParam(required = false, defaultValue = "10") Integer pageSize){
        if (!Objects.equals(id, Constants.PLATFORM)) {
            throw new BusinessException(ReturnNo.AUTH_NO_RIGHT, String.format(ReturnNo.AUTH_NO_RIGHT.getMessage(), "查询评论"));
        }
        List<Comment> commentList = commentService.findCommentsByStatus(status, page, pageSize);
        return new ReturnObject(ReturnNo.OK, new PageVo<>(commentList.stream().map(o -> CloneFactory.copy(new CommentVo(o), o)).toList(), page, pageSize));
    }

}
